<template>
  <div class="classroom-header" :class="{'home-header': isHome}">
    <div class="classroom-header-left">
      <div class="title" @click="toHome">线上教学课堂</div>
      <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal"
               ref="elMenu" @select="handleSelect">
        <el-menu-item index="onlineStudy"><span>在线学习</span><img src="../assets/icons/arrow-down.png"></el-menu-item>
        <el-menu-item index="studyRecord"><span>学习记录</span><img src="../assets/icons/arrow-down.png"></el-menu-item>
        <el-menu-item index="studentInfo"><span>学生管理</span><img src="../assets/icons/arrow-down.png"></el-menu-item>
        <el-menu-item index="studyResource"><span>学习资源上传</span><img src="../assets/icons/arrow-down.png"></el-menu-item>
      </el-menu>
    </div>
    <div class="classroom-header-right" @click="noTokenToLogin">
      <el-dropdown :class="['avatar-container']" trigger="click" :hide-on-click="hideDrop">
        <div id="userImg" class="avatar-wrapper turn-color">
          <el-image
            style="width: 24px; height: 24px; border-radius: 50%;"
            :src="headPortraits"
          >
            <div slot="error" class="image-slot">
              <img src="../assets/icons/avatar-icon.png" class="user-avatar">
            </div>
          </el-image>
          <span id="username" class="username">{{ name }}</span>
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown" v-if="hasToken">
          <el-dropdown-item>
            <el-dropdown trigger="hover"  placement="left" @command="toggleStudent">
              <span style="padding:6px 17px">切换身份</span>
              <el-dropdown-menu>
                <el-dropdown-item v-for="student in studentList" :key="student.id" :command="student"
                                  :class="{'classroom-active-downmenu': student.id == studentInfo.id}" style="padding: 0 17px">
                  <span>{{ student.studentName }}</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-dropdown-item>
          <el-dropdown-item divided>
            <span style="padding:6px 17px" @click="userCenterFlag()">个人中心</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span style="padding:6px 17px;">{{ $t('navbar.logOut') }}</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/framework/utils/auth'

const routerPaths = {
  onlineStudy: '/onlineClassroom/course',
  studentInfo: '/onlineClassroom/studentInfo',
  studyRecord: '/onlineClassroom/browsingHistory',
  studyResource: '/onlineClassroom/myUpload',
  personCenter: '/onlineClassroom/updatePassword'
}

export default {
  name: 'ClassroomHeader',
  data() {
    return {
      activeIndex: '',
      activeStudent: '',
      hideDrop: false
    }
  },
  computed: {
    ...mapGetters(['name', 'onlineStudy', 'studentInfo', 'studentList']),
    isHome() {
      return this.$route.path == '/onlineClassroom/home'
    },
    headPortraits() {
      return this.$store.state.user.avatar
    },
    hasToken() {
      return Boolean(getToken())
    }
  },
  watch: {
    '$route.meta': {
      deep: true,
      immediate: true,
      handler(meta) {
        this.activeIndex = meta.topMenu
      }
    }
  },
  mounted() {
    this.$store.dispatch('classroom/changeOnlineStudy')
  },
  methods: {
    toggleStudent(info) {
      this.$store.commit('classroom/CHANGE_STUDENT_INFO', info)
    },
    toHome() {
      this.$refs.elMenu.closeMenu(this.$refs.elMenu.openedMenus[0])
      this.$refs.elMenu.activeIndex = null
      if (!this.isHome) {
        this.$router.push({
          path: '/onlineClassroom/home'
        })
      }
    },
    handleSelect(val) {
      if (val == 'onlineStudy') {
        this.$router.push({
          path: this.onlineStudy[0].path
        })
        return
      }
      this.$router.push({
        path: routerPaths[val]
      })
    },
    userCenterFlag() {
      // 跳转个人中心
      this.hideDrop = true
      setTimeout(() => {
        this.hideDrop = false
      }, 800)
      if (this.$route.path != '/onlineClassroom/updatePassword') {
        this.$router.push({
          path: '/onlineClassroom/updatePassword'
        })
      }
    },
    logout() {
      this.$confirm('是否退出登录？', '退出', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async () => {
        await this.$store.dispatch('user/logout')
        sessionStorage.removeItem('studentList')
        sessionStorage.removeItem('studentInfo')
        this.$router.push('/classroomLogin')
      }).catch(() => {
        // 取消
      })
    },
    noTokenToLogin() { // 未登录去登录
      if (!this.hasToken) {
        this.$router.push('/classroomLogin')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.classroom-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 80px;
  background-color: #fff;
  padding: 0 52px;
  box-shadow: 0 3px 6px #b9ceed;
  z-index: 999;
  &-left {
    display: flex;
    .el-menu-demo {
      width: 600px;
      .el-menu-item {
        height: 80px !important;
        line-height: 76px !important;
      }
    }
  }
  &-right {
    // height: 100%;
    // line-height: 80px;
    .avatar-container {
      // margin-right: 30px;
      margin-top: 5px;
      padding-left: 8px;
      .avatar-wrapper {
        // margin-top: 5px;
        position: relative;
        display: flex;
        align-items: center;
        cursor: pointer;

        .user-avatar {
          cursor: pointer;
          width: 24px;
          height: 24px;
          border-radius: 50%;
        }
        .username {
          margin: 0 0 0 6px;
          font-family: "Source Han Sans CN";
          font-weight: 400;
          font-size: 20px;
          text-align: left;
          color: #00080f;
        }
        @media only screen and (max-width: 1164px) {
          .username {
            display: none;
          }
        }
        .el-icon-arrow-down {
          cursor: pointer;
          position: absolute;
          right: -16px;
          // top: 20px;
          font-size: 12px;
        }
      }
    }
  }
  .title {
    font-family: "YouSheBiaoTiHei";
    font-weight: 400;
    font-size: 28px;
    text-align: left;
    color: #004fff;
    margin-right: 97px;
    white-space: nowrap;
    line-height: 80px;
    cursor: pointer;
  }
  ul {
    margin-block-start: 0;
    margin-block-end: 0;
    padding-inline-start: 0;
  }
  .el-menu-item {
    position: relative;
    font-family: "Source Han Sans CN";
    font-weight: 400;
    font-size: 20px;
    color: #00080f;
    border-bottom-color: transparent;
    border-bottom: none;
    span {
      position: relative;
      display: inline-block;
      margin-right: 6px;
    }
    img {
      width: 9.9px;
    }
  }
  .is-active {
    span::after {
      content: '';
      position: absolute;
      bottom: -4px;
      left: 0;
      display: inline-block;
      width: 100%;
      height: 4px;
      border-radius: 2px;
      background-color: var(--primary)
    }
  }
}
.home-header {
  padding: 0 226px;
  box-shadow: none;
}
@media screen and  (min-width: 901px) and (max-width: 1382px) {
  .classroom-header {
    padding: 0 130px;
    .title {
      margin-right: 60px;
    }
    .el-menu-item {
      font-size: 18px; /* 减小字体大小 */
      padding: 0 10px; /* 减少内边距 */
    }
  }
}
@media screen and (max-width: 900px) {
  .classroom-header {
    padding: 0 50px;
    .title {
      display: none;
    }
    .el-menu-item {
      font-size: 18px; /* 减小字体大小 */
      padding: 0 10px; /* 减少内边距 */
    }
  }
}
</style>
<style>
.classroom-active-downmenu {
  background-color: rgba(52, 97, 255, 0.1) !important;
  color: rgba(52, 97, 255, 0.8) !important;
}
</style>
